//
//  LXJMainController.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/26.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SVProgressHUD
import SnapKit

class LXJMainController: UITabBarController,LXJTabBarDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        /// 界面搭建
        setupUI()
    }
    
    
    // MARK: 页面搭建
    func setupUI() -> Void {
        //        self.tabBar = LXJTabBar()/// 只读属性
        let lxjTabBar = LXJTabBar()
        
        /// 赋值代理对象
//        lxjTabBar.lxj_delegate = self
        /// 赋值闭包对象
        lxjTabBar.publishWeiBoActionClosure = {[weak self] in
            /// 判断用户是否登录成功
            if !LXJOAuthViewModel.sharedOAuthViewModel.isLogin {
                /// 提示用户没好友登录
                SVProgressHUD.showError(withStatus: "亲,您还没有登录")
                return
            }
            /// 登录后弹出发微博的视图
            let composeView = LXJComposeView()
            composeView.showMaskView(target: self!)
        }
        
        self.setValue(lxjTabBar, forKey: "tabBar")
        
        /// 添加子控制器
        addChildViewController(childController: LXJHomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: LXJMessageController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: LXJDiscoveryController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: LXJProfileController(), title: "我的", imageName: "tabbar_profile")
    }
    
    // MARK: 重载 addChildVC 的方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        /// 设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        /// 设置文字
        childController.title = title
        /// 文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .normal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        /// 文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        /// 如果想修改一些选中的属性,想办法使用它的代理方法
        //        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .selected)
        
        /// 设置导航控制器
        let navVC = LXJNavigationController(rootViewController: childController)
        
        /// 添加子视图控制器
        addChildViewController(navVC)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: 实现 tabBar 的代理方法
    func publishWeiBoAction() {
        print("实现 tabBar 的代理方法,实现发微博的功能")
        
    }  
}
